const mongoose=require('mongoose');
const bcrypt=require('bcrypt');
const LocalStrategy=require('passport-local').Strategy;

//加载model
const User = mongoose.model("users");

module.exports=(passport)=>{
    passport.use(new LocalStrategy(
        {usernameField:"email"},
        (email,password,done)=>{
            User.findOne({email:email})
                .then((user)=>{
                    if(!user){
                        return done(null,false,{message:"没有这个用户！"});
                    }
                    //密码验证
                    bcrypt.compare(password,user.password,(err,isMatch)=>{
                        if(err) throw err;
                        if(isMatch){
                            return done(null,user)
                        }else {
                            return done(null,false,{message:"密码错误！"});
                        }
                    })
                });
        }
        ))
    passport.serializeUser((user,done)=>{
        done(null,user.id);
    });
    passport.deserializeUser((id,done)=>{
        User.findById(id,(err,user)=>{
            done(err,user);
        });
    });
}